// pages/analysis/analysis.js

const TimeUtils = require('../../utils/timeUtils')
const lunarCalendar = require('../../utils/calendar')
var address = require('../../utils/city.js')
import request from "../../utils/request.js";
import auth from "../../utils/auth.js";
import local from "../../utils/local";
import nav from "../../utils/nav.js";
import toast from "../../utils/toast.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appid: 'wx15a7538fa77ddfc0',
    title: '',
    name: '',
    sexType: 'man',
    positionType: 'center',
    position: '',
    birthDay: '',
    birthAddress: '',
    birthTime: '',
    timeType: 'ts',
    birthDayVisible: false,
    birthAddressVisible: false,
    birthDayType: 'gregorian',

    birthDayYear: '1980', 
    birthDayMonth: '6', 
    birthDayDay: '15', 
    birthDayHour: '12', 
    birthDayMin: '30', 

    birthDayLYear: '1980',
    birthDayLMonth: '五', 
    birthDayLDay: '初三', 
    birthDayLHour: '12午', 
    birthDayLMin: '30',

    selectDateTime: '',

    minTime: '1901/01/01',
    maxTime: '2100/12/31',
    selectTime: '',
    isLunar: false,
    titleActive: 0, // 0 公历  1 农历

    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    areaInfo: '',
    surName: '',
    custName: '',
    memberID: '',
    isTaiYang: '0',
    isZaoWanZiShi: '0',
    isXiaLingShi: '0',
    sex: '',
    csYear: '',
    csMonth: '',
    csDay: '',
    csHour: '',
    csMinute: '',
    remark: '',
    counName: '',
    provName: '',
    cityName: '',
    areaName: '',

    amo: '',
    juanCode: '',
    juanAmo: '',
    msAmo: '',
    settID: '',
    payAmo: '',
    transID: '',
    paiPanItemsCode: '',
    baZi: '',
    genCont: '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let dataStr = decodeURIComponent(options.data)
    let params = JSON.parse(dataStr)
    const memberID = local.getLocal('memberID');
    this.setData({
      title: params.title,
      memberID: memberID
    })
    console.log(params.title)

    const selectDateTime = this.data.birthDayYear + "/" + this.data.birthDayMonth + "/" + this.data.birthDayDay + " " + this.data.birthDayHour + ":" + this.data.birthDayMin

    var id = address.provinces[0].id
    this.setData({
      selectDateTime: selectDateTime,
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })

  },

  clickToSelectSex(e) {
    const id = e.currentTarget.id;
    if (this.data.sexType === id) {
      return;
    }
    this.setData({
      sexType: id,
    })
  },

  clickToSelectPosition(e) {
    const id = e.currentTarget.id;
    if (this.data.positionType === id) {
      return;
    }
    this.setData({
      positionType: id,
    })
  },

  clickToSelectBirthDay(e) {
    this.setData({
      birthDayVisible: true,
    })
  },

  clickToSelectTime(e) {
    const id = e.currentTarget.id;
    if (this.data.timeType === id) {
      return;
    }
    this.setData({
      timeType: id,
    })
  },

  clickToBrithDayCancel() {
    this.setData({
      birthDayVisible: false,
    })
  },

  clickToSelectBirthDayType(e) {
    const id = e.currentTarget.id;
    if (this.data.birthDayType === id) {
      return;
    }
    this.setData({
      birthDayType: id,
      pickerIsLunar: id === 'lunar',
    })

    this.initData(true)
  },

  handleToAnalysis(e) {
    this.getPaipanitemsservice1()
    // console.log(e);
    // const id = e.currentTarget.id;
    // let tempDic = {
    //   id: id,
    //   title: '',
    // }
    // let jsonStr = JSON.stringify(tempDic)
    // let data = encodeURIComponent(jsonStr)
    // nav.navigateTo('/detail/detail?data='+data)
  },

  handleToRecord(e) {
    nav.navigateTo('/record/record')
  },

  bindKeyInput(e) {
    let value = e.detail.value || "";
    if (e.currentTarget.id == "surName") {
      this.setData({
        surName: value
      });
    } else if (e.currentTarget.id == "name") {
      this.setData({
        name: value
      });
    }else if (e.currentTarget.id == "remark") {
      this.setData({
        remark: value
      });
    }
  },

  getPaipanitemsservice1() {
    if (this.data.title == '姓名解析') {
      if (this.data.birthDay.length == 0) {
        toast.showToast('请选择出生时间')
        return
      }
      if (this.data.birthAddress.length == 0) {
        toast.showToast('请选择出生地址')
        return
      }
    }else {
      if (this.data.birthDay.length == 0) {
        toast.showToast('请选择出生时间')
        return
      }
    }
    
    const that = this;
    let tempSex = ''
    let tempCustName = this.data.surName + this.data.name
    if (this.data.sexType == 'man') {
      tempSex = '男'
    }else {
      tempSex = '女'
    }
    let tempRemark = ''
    if (this.data.positionType == 'center') {
      tempRemark = '中间字' + this.data.remark
    }else {
      tempRemark = '末尾字' + this.data.remark
    }
    this.setData({
      sex: tempSex,
      custName: tempCustName,
    })
    let userInfoDic = {
      "user": this.data.appid,
      "eventCode": "301"
    }
    let paiPanItemsStuctDic = {
      "surName": this.data.surName,
      "custName": this.data.custName,
      "memberID": this.data.memberID,

      "isTaiYang": this.data.isTaiYang,
      "isZaoWanZiShi": this.data.isZaoWanZiShi,
      "isXiaLingShi": this.data.isXiaLingShi,
      "sex": this.data.sex,

      "csYear": this.data.birthDayYear,
      "csMonth": this.data.birthDayMonth,
      "csDay": this.data.birthDayDay,
      "csHour": this.data.birthDayHour,
      "csMinute": this.data.birthDayMin,
      "counName": this.data.counName,
      "provName": this.data.provName,
      "cityName": this.data.cityName,
      "areaName": this.data.areaName,
      "remark": tempRemark
    }
    let dic = {
      userInfo: userInfoDic,
      paiPanItemsStuct: paiPanItemsStuctDic,
    }
    console.log(dic)
    request.get('/paipanitemsservice',{param: dic}).then((res) => {
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        const object = res.PAIPAN_INFO
        // const BAZI_INFO_string = res.BAZI_INFO
        // var baZhiObject = JSON.parse(BAZI_INFO_string)
        // const result = baZhiObject.result
        // console.log(result)
        that.setData({
          paiPanItemsCode: object.paiPanItemsCode,
          baZi: object.baZi,
          // amo: object.amo,
          // juanCode: object.juanCode,
          // juanAmo: object.juanAmo,
          // msAmo: object.msAmo,
          // settID: object.settID,
          // payAmo: object.payAmo,
          // transID: object.transID,
        })
        // if (that.data.amo == 0) {
          that.getPaipanitemsservice2()
        // }else {
        //   that.getPaipanitemsservicePay()
        // }
      }else {
        const msg = res.MSG
        toast.showToast(msg)
      }
    })
  },

  getPaipanitemsservice2(e) {
    const that = this;
    let tempSex = ''
    if (this.data.sexType == 'man') {
      tempSex = '男'
    }else {
      tempSex = '女'
    }
    this.setData({
      sex: tempSex,
    })
    let userInfoDic = {
      "user": this.data.appid,
      "eventCode": "302"
    }
    let paiPanItemsStuctDic = {
      "paiPanItemsCode": this.data.paiPanItemsCode,
      "baZi": this.data.baZi,
      "memberID": this.data.memberID,
      "sex": this.data.sex
    }
    let dic = {
      userInfo: userInfoDic,
      paiPanItemsStuct: paiPanItemsStuctDic,
    }
    console.log("================")
    console.log(dic)
    request.get('/paipanitemsservice',{param: dic}).then((res) => {
      console.log("=========33333333=========")
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        const object = res

        let jsonStr = JSON.stringify(object)
        let data = encodeURIComponent(jsonStr)
        nav.navigateTo('/detail/detail?data='+data)
      }
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 点击所在地区弹出选择框
  clickToSelectBirthAddress: function (e) {
    var that = this
    // 如果已经显示，不在执行显示动画
    if (that.data.addressMenuIsShow) {
      return
    }
    // 执行显示动画
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow)
    var that = this
    // if (isShow) {
    //   // vh是用来表示尺寸的单位，高度全屏是100vh
    //   that.animation.translateY(0 + 'vh').step()
    // } else {
    //   that.animation.translateY(40 + 'vh').step()
    // }
    that.setData({
      // animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮 
  citySure: function (e) {
    var city = this.data.city
    var value = this.data.value
    this.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = this.data.provinces[value[0]].name  + this.data.citys[value[1]].name + this.data.areas[value[2]].name
    console.log(areaInfo)
    this.setData({
      counName: '中国',
      provName: this.data.provinces[value[0]].name,
      cityName: this.data.citys[value[1]].name,
      areaName: this.data.areas[value[2]].name,
      birthAddress: areaInfo,
    })
  },
  // 点击蒙版时取消组件的显示
  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },
  // 处理省市县联动逻辑
  cityChange: function (e) {
    console.log(e)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      // 滑动选择了区
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
    console.log(this.data)
  },

  onUpdateLunarTime(e) {
    console.log("=====onUpdateLunarTime======")
    console.log(e)
    const detail = e.detail
    const date = detail.date
    const time = detail.time
    console.log(time)
    const timeObject = detail.timeObject
    console.log(timeObject)
    console.log("=======timeObject========")
    const year = date.substring(0,4)
    const month = date.substring(5,7)
    const day = date.substring(8,10)
    let hour = ''
    let min = ''
    let lhour = ''
    let lmin = ''
    // if (this.data.titleActive == 0) {
      hour = time.substring(0,2)
      min = time.substring(3,5)
    // }else {
    //   lhour = time.substring(0,2)
    //   lmin = time.substring(3,5)
    // }
    // const hour = time.substring(0,2)
    // const min = time.substring(3,5)
    const lYear = timeObject.lYear
    const IMonthCn = timeObject.IMonthCn
    const IDayCn = timeObject.IDayCn

    let lunarHour = ''
    if(hour == 0 || hour == 23) {
      lunarHour = '子'
    }
    if(hour == 1 || hour == 2) {
      lunarHour = '丑'
    }
    if(hour == 3 || hour == 4) {
      lunarHour = '寅'
    }
    if(hour == 5 || hour == 6) {
      lunarHour = '卯'
    }
    if(hour == 7 || hour == 8) {
      lunarHour = '辰'
    }
    if(hour == 9 || hour == 10) {
      lunarHour = '巳'
    }
    if(hour == 11 || hour == 12) {
      lunarHour = '午'
    }
    if(hour == 13 || hour == 14) {
      lunarHour = '未'
    }
    if(hour == 15 || hour == 16) {
      lunarHour = '申'
    }
    if(hour == 17 || hour == 18) {
      lunarHour = '酉'
    }
    if(hour == 19 || hour == 20) {
      lunarHour = '戌'
    }
    if(hour == 21 || hour == 22) {
      lunarHour = '亥'
    }
  
    
    this.setData({
      birthDayYear: year, 
      birthDayMonth: month, 
      birthDayDay: day, 
      birthDayHour: hour, 
      birthDayMin: min, 

      birthDayLYear: lYear,
      birthDayLMonth: IMonthCn,
      birthDayLDay: IDayCn,
      birthDayLHour: hour + lunarHour,
      birthDayLMin: min
    })
    console.log("设置时间 ===== ")
    console.log(this.data.birthDayLYear)
  },

  // 选择公历还是阴历类型
  onUpdateLunarTimeType(e) {
    console.log(e)
    const detail = e.detail
    const titleActive = detail.titleActive
    this.setData({
      titleActive: titleActive,
    })
  },

  // 点击确定按钮
  clickToBrithDayConfirm: function (e) {
    let time = ''
    if (this.data.titleActive == 0) {
      time = "公历:" + this.data.birthDayYear + "年" + this.data.birthDayMonth + "月" + this.data.birthDayDay + "日 " + this.data.birthDayHour + "时" + this.data.birthDayMin + "分"
    }else {
      time = "阴历:" + this.data.birthDayLYear + "年" + this.data.birthDayLMonth + this.data.birthDayLDay + " " + this.data.birthDayLHour + "时" + this.data.birthDayLMin + "分"
    }
    
    this.setData({
      birthDayVisible: false,
      birthDay: time
    })
  },
  
})